package cn.s1995.sloth.bizs.service.impl;

import cn.s1995.sloth.bizs.constant.CacheNames;
import cn.s1995.sloth.bizs.entity.OrderItem;
import cn.s1995.sloth.bizs.entity.OrderRefund;
import cn.s1995.sloth.bizs.entity.Sku;
import cn.s1995.sloth.bizs.mapper.OrderItemMapper;
import cn.s1995.sloth.bizs.oo.vo.OrderItemVO;
import cn.s1995.sloth.bizs.service.OrderItemService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * 订单项
 *
 * @author sloth
 * @date 2023-12-15 15:31:58
 */
@Service
public class OrderItemServiceImpl extends ServiceImpl<OrderItemMapper, OrderItem> implements OrderItemService {


	@Override
	@Deprecated
	public List<OrderItemVO> selectByOrderId(Long orderId) {
		MPJLambdaWrapper<OrderItem> wrapper = new MPJLambdaWrapper<OrderItem>()
	            .selectAll(OrderItem.class)
	            .selectAssociation(OrderRefund.class, OrderItemVO::getOrderRefund)
	            .selectAs(Sku::getProperties, OrderItemVO::getProperties)
	            .leftJoin(OrderRefund.class, OrderRefund::getOrderItemId, OrderItem::getId)
	            .leftJoin(Sku.class, Sku::getId, OrderItem::getSkuId)
	            .eq(OrderItem::getOrderId, orderId);
		return baseMapper.selectJoinList(OrderItemVO.class, wrapper);
	}

	@Override
	@Cacheable(cacheNames = CacheNames.orderItems, key = "#orderId")
	public List<OrderItem> getOrderItemsByOrderId(Long orderId) {
		return this.list(
				Wrappers.lambdaQuery(OrderItem.class)
						.eq(OrderItem::getOrderId, orderId)
		);
	}
}
